<?php
namespace app\common\service;

/**
 * @name: 配置
 * @Param: 
 * @Description: 
 * @Author: foo
 * @Date: 2019-10-23 14:42:46
 */
class Config extends Base 
{
    public $config_path = '';//配置路径
    public $config_type = '';//配置路径
    public $dir = '../runtime/config/';//配置路径
    /**
     * @name: mycat 处理输出
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-10-23 16:15:54
     */
    function mycatNodeUpdate_fGet ()
    {

        $return['is_mycat_update'] = file_exists($this->config_path) ? 1 : 0;

        return $return;
    }

    /**
     * @name: 检测是否安装
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-10-24 10:32:12
     */
    function checkInstall_fGet ()
    {
        $return['is_init'] = file_exists($this->config_path) ? 1 : 0;

        return $return;
    }


    /**
     * @name: 文件写入
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-10-23 16:46:18
     */
    function fWrite ($data)
    {
        //status转化为bool值
        if(isset($data['status'])){
            if ($data['status'] == 1 || $data['status'] == 'true') {
                $data['status'] = true;
            }else{
                $data['status'] = false;
            }
        }

        //2. 写入配置
        file_put_contents($this->config_path, json_encode($data));
    }

    /**
     * @name: 配置获取
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-09-20 16:02:25
     */
    function fGet ()
    {
        $contnet = file_get_contents($this->config_path);
        $return = json_decode($contnet,true);

        return $return;
    }

    /**
     * @name: 文件检测存在
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-10-23 16:47:29
     */
    function fCheck ()
    {

        if(!file_exists($this->config_path))
        {
            //创建目录
            $num = strrpos($this->config_path, '/');
            $path = substr($this->config_path, 0, $num);
            if (!file_exists($path))
            {
                mkdir($path, 0777, true);
            }


            //创建文件
            $server = new \app\common\model\Config();
            $params = $this->toDo([$server, $this->config_type]);

            $data = [];
            foreach ($params as $v){
                $data[$v] = '';
            }


            file_put_contents($this->config_path, json_encode($data));

        }

        return true;
    }

    /**
     * @name: 需要处理写入
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-10-23 17:14:34
     */
    function fGetProcess ()
    {
        $method = $this->config_type . '_fGet'; 
        if(method_exists($this, $method))
        {
            return $this->toDo([$this, $method]);
        }
        else
        {
            $this->toDo([$this, 'fCheck']);
            return $this->toDo([$this, 'fGet']);
        }

    }

    /**
     * @name: 处理写入
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-10-23 17:51:49
     */
    function fWriteProcess ($data)
    {
        $method = $this->config_type . '_fWrite';

        if(method_exists($this, $method))
        {
            $this->toDo([$this, $method], [$data]);
        }
        else
        {
            $this->toDo([$this, 'fWrite'], [$data]);
        }
    }

    /**
     * @name ip白名单配置
     * @param $data
     * @author wx
     * @date 2019/10/28 14:58
     */
    function ipWhiteSetting_fWrite($data)
    {
        //1. 写入时间
        $data['start_time'] = time();

        //2. 写入配置
        file_put_contents($this->config_path, json_encode($data));
    }

}